#include "dominion.h"
#include "dominion_helpers.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getCost(int cardNumber);
int cardEffect(int card, int choice1, int choice2, int choice3, struct gameState *state, int handPos, int *bonus);

int testSalvager(int currentPlayer, int handPos, int choice1, struct gameState* state)
{
	int numBuys = state->numBuys;
	int handCount;
	
	//+1 buy
	state->numBuys++;
		
	if (state->numBuys == numBuys + 1)
		printf("salvager(): PASS when test adding 1 buy\n");
	else
		printf("salvager(): FAIL when test adding 1 buy\n");
		
	if (choice1)
	{
		int coins = state->coins;
		int cost = getCost( handCard(choice1, state) );
		
		//gain coins equal to trashed card
		state->coins = state->coins + getCost( handCard(choice1, state) );
		
		if (state->coins == coins + cost)
			printf("salvager(): PASS when test gaining coins equal to trashed card\n");
		else
			printf("salvager(): FAIL when test gaining coins equal to trashed card\n");

		handCount = state->handCount[currentPlayer];
		
		//trash card
		discardCard(choice1, currentPlayer, state, 1);
	
		if (state->handCount[currentPlayer] == handCount - 1)
			printf("salvager(): PASS when test trashing 1 card\n");
		else
			printf("salvager(): FAIL when test trashing 1 card\n");
	}

	handCount = state->handCount[currentPlayer];

	//discard card
	discardCard(handPos, currentPlayer, state, 0);

	if (state->handCount[currentPlayer] == handCount - 1)
		printf("salvager(): PASS when test discard 1 card\n");
	else
		printf("salvager(): FAIL when test discard 1 card\n");
	
	return 0;
}

void test()
{
	struct gameState g;
	struct gameState * game = &g;
	memset(game, 0, sizeof(struct gameState));
	int choice1;
	int numBuys;
	int coins;
	int cost;
	
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
		sea_hag, tribute, smithy};
	initializeGame(4, k, 2, game);
	int handPos = 0;
	int coin_bonus = 0;
	
	choice1 = 0;	
	printf("salvager(): testing choice1 = 0:\n");
	numBuys = game->numBuys;
	cardEffect(salvager, choice1, 0, 0, game, handPos, &coin_bonus);
	
	if (game->numBuys == numBuys + 1)
		printf("salvager(): PASS when test adding 1 buy\n");
	else
		printf("salvager(): FAIL when test adding 1 buy\n");
	
	choice1 = 1;
	game->hand[whoseTurn(game)][choice1] = adventurer;
	numBuys = game->numBuys;	
	coins = game->coins;
	cost = getCost(adventurer);
	printf("salvager(): testing choice1 = 1 for adventurer:\n");
	cardEffect(salvager, choice1, 0, 0, game, handPos, &coin_bonus);
	
	if (game->numBuys == numBuys + 1)
		printf("salvager(): PASS when test adding 1 buy\n");
	else
		printf("salvager(): FAIL when test adding 1 buy\n");
		
	if (game->coins == coins + cost)
		printf("salvager(): PASS when test gaining coins equal to trashed card\n");
	else
		printf("salvager(): FAIL when test gaining coins equal to trashed card\n");
	
	choice1 = 1;
	game->hand[whoseTurn(game)][choice1] = smithy;	
	numBuys = game->numBuys;
	coins = game->coins;
	cost = getCost(smithy);
	printf("salvager(): testing choice1 = 1 for smithy:\n");
	cardEffect(salvager, choice1, 0, 0, game, handPos, &coin_bonus);	

	if (game->numBuys == numBuys + 1)
		printf("salvager(): PASS when test adding 1 buy\n");
	else
		printf("salvager(): FAIL when test adding 1 buy\n");

	if (game->coins == coins + cost)
		printf("salvager(): PASS when test gaining coins equal to trashed card\n");
	else
		printf("salvager(): FAIL when test gaining coins equal to trashed card\n");		
}

int main(int argc, char *argv[])
{
	test();
	return 0;
}
